package cn.itsource.celljava.system.login.controller;

import cn.itsource.celljava.constants.BaseConstants;
import cn.itsource.celljava.config.BussnisEptional;
import cn.itsource.celljava.system.employee.domain.EmployeeBo;
import cn.itsource.celljava.system.logininfo.domain.Logininfo;
import cn.itsource.celljava.system.logininfo.service.ILogininfoService;
import cn.itsource.celljava.utils.AjaxResult;
import cn.itsource.celljava.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/login")
@Api(value = "登录",description = "登录")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("/shop")
    @ApiOperation("商家登录")
    public AjaxResult shop(@RequestBody Logininfo logininfo){
        try {
            EmployeeBo emp = logininfoService.findToLogin(logininfo);
            //没有异常说明用户登录成功
            //生成一个token
            String token = StrUtils.getRandomString(128);
            //存入redis,设置过期时间
            redisTemplate.opsForValue().set(token,emp, 30, TimeUnit.MINUTES);
            HashMap<String, Object> h = new HashMap<>();
            h.put("token",token);
            h.put("user",emp);
            return AjaxResult.success().setData(h).setMsg("userLogin");
        } catch (BussnisEptional e) {
            e.printStackTrace();
            return AjaxResult.fail().setMsg(e.getMessage());
        }
    }
}
